<!--
  File Name: LeaveFormController.php
  Version 1.0
  O1-31-2011
  
  As of 02-05-2011, we finished Approved with pay!!!! :)
-->

<?php
require_once('LeaveManager.php');
require_once('LeaveFormView.php');
require_once('functions.php');

class LeaveFormController
{
	public function addLeaveForm($dateOfFiling, $dateReceived,
									$typeOfLeave, $days, $startDate, $endDate,
									$status, $employeeNumber, $time){
		if($dateOfFiling == "" && $dateReceived == "") {
			
			$dateOfFiling = "NULL";
			$dateReceived = "NULL";
		}
		else if($dateOfFiling == "" && $dateReceived != "") {
			$dateOfFiling = "NULL";
			$dateReceived = transformDate($dateReceived);
		} else if ($dateReceived == "" && $dateOfFiling != "") {
			$dateOfFiling = transformDate($dateOfFiling);
			$dateReceived = "NULL";
		} else {
			$dateOfFiling = transformDate($dateOfFiling);
			$dateReceived = transformDate($dateReceived);		
		}

		$startDate = transformDate($startDate);
		$endDate = transformDate($endDate);
		
		//transform number of days
		$result = LeaveManager::insertLeaveForm($dateOfFiling, $dateReceived,
									$typeOfLeave, $days, $startDate, $endDate,
									$status, $employeeNumber, $time);
		
		
		return true;
		
	}
	
	public function generateLeaveList($employeeNumber) {
		$data = LeaveManager :: getLeaveData($employeeNumber, "load");
		LeaveFormView :: showLeaveList($data, $employeeNumber);
		return 1;
	}
	
	public function editLeaveForm($leaveNumber, $dateOfFiling, $dateReceived,
									$typeOfLeave, $days, $startDate, $endDate,
									$status, $time, $employeeNumber){
								 											
		if($dateOfFiling == "" && $dateReceived == "") {
			
			$dateOfFiling = "NULL";
			$dateReceived = "NULL";
		}
		else if($dateOfFiling == "" && $dateReceived != "") {
			$dateOfFiling = "NULL";
			$dateReceived = transformDate($dateReceived);
		} else if ($dateReceived == "" && $dateOfFiling != "") {
			$dateOfFiling = transformDate($dateOfFiling);
			$dateReceived = "NULL";
		} else {
			$dateOfFiling = transformDate($dateOfFiling);
			$dateReceived = transformDate($dateReceived);		
		}
		$startDate = transformDate($startDate);
		$endDate = transformDate($endDate);
		$result = LeaveManager::updateLeaveForm($leaveNumber, $dateOfFiling,
									$dateReceived, $typeOfLeave, $days, 
									$startDate, $endDate, $status, $time,
									$employeeNumber);
		
			
		if(!$result) return 0;
		else return 1;
	}
			
	public function subtractDate($date1, $date2, $option) {
		$year1=substr($date1, 0, 4);
		$month1=substr($date1, 5, 2);
		$day1=substr($date1, 8, 2);
		$year2=substr($date2, 0, 4);
		$month2=substr($date2, 5, 2);
		$day2=substr($date2, 8, 2);
		
		if($day1<$day2){
			$day1+=30;
			$month1-=1;
		}
		$days=$day1-$day2;
		
		if($month1<$month2){
			$month1+=12;
			$year1-=1;
		}
		$months=$month1-$month2;
		$years=$year1-$year2;

		if($option=="year") {
			return $years;
		}else if($option=="month"){
			return $months;
		}else{
			return $days;
		}	
	}
	
	public function computeLeaveBalance($employeeNumber){
		$link = makeSQLconnection();		
		
		$flag = 0;
		$r1 = LeaveManager :: getLeaveData($employeeNumber, "compute");//from employee table
		$r2 = LeaveManager :: getLeaveData($employeeNumber, "load");//from leaveform table
		$r3 = LeaveManager :: getLeaveData($employeeNumber, "load");//from leaveform table
		$daysInMonth = array(31,28,31,30,31,30,31,31,30,31,30,31);		
		
		$accumulatedExcess = 0;
		$sickLeave = 0;
		$vacationLeave = 0;
		$balanceSickLeave = 0;
		$balanceVacationLeave = 0;
		$excess = 0;
		$accumulatedSickDays = 0;
		$accumulatedVacDays = 0;
		$rowCounter = 1;
		$accumulatedSpecial = 0;
		$accumulatedPaternal = 0;
		$accumulatedMaternal = 0;
		$accumulatedReEnergizing = 0;
		$accumulatedMonetization = 0;
		$accumulatedVacWo = 0;
		$accumulatedSicWo = 0;
		$accumulatedForced = 0;
		$accumulatedUndertime = 0;
		$accumulatedTardiness = 0;
		$accumulatedExcessSpecial = 0;
		$endDate = "";
		
		$data1 = mysql_fetch_assoc($r1);
		$originalAppointment = $data1['Original_appointment'];
		
		
		if ($data2 = mysql_fetch_assoc($r2)) {
		
				while ($data2 = mysql_fetch_assoc($r3))	{
					$endDate = $data2['End_date'];
					$startDate = $data2['Start_date'];
					$time = $data2['Hours_minutes'];
					$timeCredits = 0;
					
					
					if ($data2['Type_of_leave'] == "SICK_LEAVE") {
						$sickLeave = $data2['Days'];
						$accumulatedSickDays += $sickLeave;
					} else if ($data2['Type_of_leave'] == "VACATION_LEAVE") {
						$vacationLeave = $data2['Days'];
						$accumulatedVacDays += $vacationLeave;											
					} else if ($data2['Type_of_leave'] == "SPECIAL"){
						$specialLeave = $data2['Days'];
						$accumulatedSpecial += $specialLeave;											
					} else if ($data2['Type_of_leave'] == "MATERNAL"){
						$maternalLeave = $data2['Days'];
						$accumulatedMaternal += $maternalLeave;											
					} else if ($data2['Type_of_leave'] == "PATERNAL"){
						$paternalLeave = $data2['Days'];
						$accumulatedPaternal += $paternalLeave;											
					} else if ($data2['Type_of_leave'] == "RE-ENERGIZING") {
						$reenergizingLeave = $data2['Days'];
						$accumulatedReEnergizing += $reenergizingLeave;
					} else if ($data2['Type_of_leave'] == "MONET_SL" || $data2['Type_of_leave'] == "MONET_VL") {
						$monetLeave = $data2['Days'];
						$accumulatedMonetization += $monetLeave;
					} else if ($data2['Type_of_leave'] == "FORCED") {
						$forcedLeave = $data2['Days'];
						$accumulatedForced += $forcedLeave;
					} else if ($data2['Type_of_leave'] == "TARDINESS") {
						$tardiLeave = $data2['Days'];
						$accumulatedTardiness += $tardiLeave;
					} else if ($data2['Type_of_leave'] == "UNDERTIME") {
						$underLeave = $data2['Days'];
						$accumulatedUndertime += $underLeave;
					}
				
					
					if ($data2['Type_of_leave'] == "UNDERTIME" || $data2['Type_of_leave'] == "TARDINESS" || $data2['Type_of_leave'] == "SICK_LEAVE" || $data2['Type_of_leave'] == "VACATION_LEAVE") {
					
					if($data2['Type_of_leave'] == "UNDERTIME") {
						$temp = explode('/', $time);
						$hours = $temp[0];
						$minutes = $temp[1];
						
						$hourCredits = (1.5*$hours)/12;
						$minuteCredits = (0.125*$minutes)/60;
						
						$timeCredits = $hourCredits + $minuteCredits;
						
					}
					$excess=0;
					$endYear=substr($endDate, 0, 4);
					$endMonth=substr($endDate, 5, 2);
					$endDay=substr($endDate, 8, 2);
					$startDay=substr($startDate, 8, 2);
					$startMonth=substr($startDate, 5, 2);
					
					$leaveNumber = $data2['Leave_number'];
					
					$year=LeaveFormController::subtractDate($endDate,$originalAppointment,
												'year');
					$month=LeaveFormController::subtractDate($endDate,$originalAppointment,
															'month');
					$day=LeaveFormController::subtractDate($endDate,$originalAppointment,
															'day');
					
					
					$yearCredits=(150*$year)/10;
					$monthCredits=(15*$month)/12;
					$dayCredits=(1.25*$day)/30;
					$tempBalance=$yearCredits+$monthCredits+$dayCredits;
				
					$balance = $tempBalance;
					if($accumulatedVacDays>$balance){
						$balanceVacationLeave=0;
						$excess = $accumulatedVacDays- intval($balance);
						
						$updateQuery = sprintf("UPDATE `LEAVEFORM` WHERE 'Leave_number'='".$leaveNumber."' SET `Excess`='".$excess."'");
						$returnQuery = mysql_query($updateQuery, $link);
						
						if($excess>=$endDay){
							$endMonth-=1;
						}
								
						if($endMonth<=0){
							$endMonth+=12;
							$endYear-=1;
						}
						$temp = $daysInMonth[($endMonth-1)];
						$endDay+=$temp;
							
						$endDay = $endDay - $excess;
						
								
						$newDate = $endYear."-".$endMonth."-".$endDay; //start date of without pay
						
				}else{
					$balanceVacationLeave=$balance-$accumulatedVacDays; 
					$balanceVacationLeave=$balanceVacationLeave - $timeCredits;
				}
				
				if($accumulatedSickDays>$balance){
					$balanceVacationLeave = $balance*2;
					$balanceSickLeave=0;
					if($balanceVacationLeave > $accumulatedSickDays){
						 $balanceVacationLeave -= $accumulatedSickDays;					
					}else{
						$excess = $accumulatedSickDays - intval($balanceVacationLeave);
						$balanceVacationLeave -= intval($balanceVacationLeave);
					}

					$updateQuery = sprintf("UPDATE `LEAVEFORM` SET `Excess`='".$excess."' WHERE `Leave_number`='".$leaveNumber."'");
					$returnQuery = mysql_query($updateQuery, $link);
					
					
					if($excess>=$endDay){
						$endMonth-=1;
					}
							
					if($endMonth<=0){
						$endMonth+=12;
						$endYear-=1;
					}
					$temp = $daysInMonth[($endMonth-1)];
					$endDay+=$temp;
						
					$endDay = $endDay - $excess;
					
					$newDate = $endYear."-".$endMonth."-".$endDay; //start date of without pay
									
				}else{
					$balanceSickLeave=$balance-$accumulatedSickDays;
				}
			}
					$rowCounter++;
				}

		}
		$ret = LeaveManager :: getLeaveData($employeeNumber, "compute");
		$data = mysql_fetch_assoc($ret);
		$gender = $data['Employee_gender'];
		
		
		echo "<link href='template.css' rel='stylesheet' type='text/css' media='screen' />
		<br/><br/><br/><br/>
		<div id='contentTable'>
		<table id='cell' border='1' color='black'>
		<tr>
			<td>&nbsp&nbsp&nbsp&nbspSick Leave Balance&nbsp&nbsp</td>
			<td style='width: 50px; text-align: right'>&nbsp;".round($balanceSickLeave,3)."&nbsp&nbsp&nbsp</td>
			<td>&nbsp&nbsp&nbsp&nbsp&nbsp&nbspVacation Leave Balance&nbsp&nbsp</td>
			<td style='width: 50px; text-align: right'>&nbsp;".round($balanceVacationLeave,3)."&nbsp&nbsp&nbsp</td>
			<td>&nbsp&nbsp&nbsp&nbsp&nbsp&nbspExcess Days&nbsp&nbsp</td>
			<td style='width: 50px; text-align: right'>".$excess."&nbsp&nbsp&nbsp</td>
		</tr>
		<tr>
			<td>&nbsp&nbsp&nbsp&nbspSpecial Leave&nbsp&nbsp</td>
			<td style='width: 50px; text-align: right'>".$accumulatedSpecial."&nbsp&nbsp&nbsp</td>
			<td>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
			if($gender=='Male'){echo "Paternal Leave";}
			else{echo "Maternal Leave";}
			echo "&nbsp&nbsp</td>
			<td style='width: 50px; text-align: right'>";
			if($gender=='Male'){echo $accumulatedPaternal;}
			else{echo $accumulatedMaternal;}
			echo "&nbsp&nbsp&nbsp</td>
			<td>&nbsp&nbsp&nbsp&nbsp&nbsp&nbspRe-energizing Leave&nbsp&nbsp</td>
			<td style='width: 50px; text-align: right'>".$accumulatedReEnergizing."&nbsp&nbsp&nbsp</td>
		</tr>
		<tr>
			<td>&nbsp&nbsp&nbsp&nbspMonetization Leave&nbsp&nbsp</td>
			<td style='width: 50px; text-align: right'>".$accumulatedMonetization."&nbsp&nbsp&nbsp</td>
			<td>&nbsp&nbsp&nbsp&nbsp&nbsp&nbspVacation Leave Without Pay&nbsp&nbsp</td>
			<td style='width: 50px; text-align: right'>".$accumulatedVacWo."&nbsp&nbsp&nbsp</td>
			<td>&nbsp&nbsp&nbsp&nbsp&nbsp&nbspSick Leave Without Pay&nbsp&nbsp</td>
			<td style='width: 50px; text-align: right'>".$accumulatedSicWo."&nbsp&nbsp&nbsp</td>
		</tr>
		<tr>
			<td>&nbsp&nbsp&nbsp&nbspForced Leave&nbsp&nbsp</td>
			<td style='width: 50px; text-align: right'>".$accumulatedForced."&nbsp&nbsp&nbsp</td>
			<td>&nbsp&nbsp&nbsp&nbsp&nbsp&nbspUndertime&nbsp&nbsp</td>
			<td style='width: 50px; text-align: right'>".round($accumulatedUndertime,3)."&nbsp&nbsp&nbsp</td>
			<td>&nbsp&nbsp&nbsp&nbsp&nbsp&nbspTardiness&nbsp&nbsp</td>
			<td style='width: 50px; text-align: right'>".round($accumulatedTardiness,3)."&nbsp&nbsp&nbsp</td>
		</tr>
		</table>
		</div>";
		mysql_close($link);
	}
	
public function computeRowBalance($leaveNumber, $employeeNumber, $accumulatedSickDays, $accumulatedVacDays){
		$r1 = LeaveManager :: getRowData($leaveNumber);
		$r2 = LeaveManager :: getLeaveData($employeeNumber, "compute");
		$data = mysql_fetch_assoc($r1);
		$data1 =  mysql_fetch_assoc($r2);
		$daysInMonth = array(31,28,31,30,31,30,31,31,30,31,30,31);		
		
		
		$originalAppointment = $data1['Original_appointment'];
		$endDate = $data['End_date'];
		$startDate = $data['Start_date'];
		$time = $data['Hours_minutes'];
		$timeCredits = 0;
		$link = makeSQLconnection();
		
			if($data['Type_of_leave'] == "UNDERTIME") {
				$temp = explode('/', $time);
				$hours = $temp[0];
				$minutes = $temp[1];
				
				$hourCredits = (1.5*$hours)/12;
				$minuteCredits = (0.125*$minutes)/60;
				
				$timeCredits = $hourCredits + $minuteCredits;
			}
			
			$excess=0;
			$endYear=substr($endDate, 0, 4);
			$endMonth=substr($endDate, 5, 2);
			$endDay=substr($endDate, 8, 2);
			$startDay=substr($startDate, 8, 2);
			$startMonth=substr($startDate, 5, 2);
			$leaveNumber = $data['Leave_number'];
			
			
			$year=LeaveFormController::subtractDate($endDate,$originalAppointment,
										'year');
			$month=LeaveFormController::subtractDate($endDate,$originalAppointment,
													'month');
			$day=LeaveFormController::subtractDate($endDate,$originalAppointment,
													'day');
			
			$yearCredits=(150*$year)/10;
			$monthCredits=(15*$month)/12;
			$dayCredits=(1.25*$day)/30;
			$tempBalance=$yearCredits+$monthCredits+$dayCredits;
		
			$balance = $tempBalance;
		if ($data['Type_of_leave'] == "UNDERTIME" || $data['Type_of_leave'] == "TARDINESS" || $data['Type_of_leave'] == "SICK_LEAVE" || $data['Type_of_leave'] == "VACATION_LEAVE") {
			if($accumulatedVacDays>$balance){
				$balanceVacationLeave=0;
				$excess = $accumulatedVacDays- intval($balance);
				
				$updateQuery = sprintf("UPDATE `LEAVEFORM` WHERE 'Leave_number'='".$leaveNumber."' SET `Excess`='".$excess."'");
				$returnQuery = mysql_query($updateQuery, $link);
				
				if($excess>=$endDay){
					$endMonth-=1;
				}
				if($endMonth<=0){
					$endMonth+=12;
					$endYear-=1;
				}
				$temp = $daysInMonth[($endMonth-1)];
				$endDay+=$temp;
					
				$endDay = $endDay - $excess;
				$newDate = $endYear."-".$endMonth."-".$endDay; //start date of without pay
							
			}else{
				$balanceVacationLeave=$balance-$accumulatedVacDays; 
				$balanceVacationLeave=$balanceVacationLeave - $timeCredits;
			}
	
			if($accumulatedSickDays>$balance){
		
				$balanceVacationLeave = $balance*2;
				$balanceSickLeave=0;
				
				if($balanceVacationLeave > $accumulatedSickDays){
					 $balanceVacationLeave -= $accumulatedSickDays;
				}else{
					$excess = $accumulatedSickDays - intval($balanceVacationLeave);
					$balanceVacationLeave -= intval($balanceVacationLeave);
				}
				$updateQuery = sprintf("UPDATE `LEAVEFORM` SET `Excess`='".$excess."' WHERE `Leave_number`='".$leaveNumber."'");
				$returnQuery = mysql_query($updateQuery, $link);
				
				if($excess>=$endDay){
					$endMonth-=1;
				}
				if($endMonth<=0){
					$endMonth+=12;
					$endYear-=1;
				}
				$temp = $daysInMonth[($endMonth-1)];
				$endDay+=$temp;
				$endDay = $endDay - $excess;
				$newDate = $endYear."-".$endMonth."-".$endDay; //start date of without pay
				
			}else{
				$balanceSickLeave=$balance-$accumulatedSickDays;
			}
		}else{
			$balanceSickLeave = $tempBalance;
			$balanceVacationLeave = $tempBalance;
		}
		$balance=$balanceSickLeave."/".$balanceVacationLeave."/".$tempBalance;
		return $balance;
	}
	
}
?>